//
//  NumberTableViewCell.m
//  BLDMusics
//
//  Created by dllo on 16/3/18.
//  Copyright © 2016年 李晓丹. All rights reserved.
//

#import "NumberTableViewCell.h"
#import "DSYSongModel.h"
#import "DSYUrlModel.h"

#define kContentW self.contentView.frame.size.width
#define kContentH self.contentView.frame.size.height

@interface NumberTableViewCell ()

@property (nonatomic, retain) UILabel *numberLabel;          /**< 音乐序号 */
@property (nonatomic, retain) UIImageView *picImgView;       /**< 图片 */
@property (nonatomic, retain) UILabel *nameLabel;            /**< 名 */
@property (nonatomic, retain) UIImageView *quityIcon;        /**< 音质的图标 */
@property (nonatomic, retain) UIImageView *mvIcon;           /**< mv的图标 */

@property (nonatomic, retain) UILabel *singer_faviroteLabel;  /**< 显示歌手和喜欢count */
@property (nonatomic, retain) UIImageView *playIcon;         /**< 播放的小图标 */

@end

@implementation NumberTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatSubviews];
    }
    return self;
}

- (void)creatSubviews {
    
    self.numberLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_numberLabel];
    
    self.picImgView = [[UIImageView alloc] init];
    [self.contentView addSubview:_picImgView];
    
    self.nameLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_nameLabel];
    
    self.quityIcon = [[UIImageView alloc] init];
    [self.contentView addSubview:_quityIcon];
    
    self.mvIcon = [[UIImageView alloc] init];
    [self.contentView addSubview:_mvIcon];
    
    self.singer_faviroteLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_singer_faviroteLabel];
    
    self.playIcon = [[UIImageView alloc] init];
    [self.contentView addSubview:_playIcon];
}


- (void)layoutSubviews
{
    _numberLabel.frame = CGRectMake(10, 10, 30, kContentH - 20);
    _numberLabel.font = [UIFont systemFontOfSize:13];
    
    _playIcon.frame = CGRectMake(kContentW - 30, 0, 20, 20);
    _playIcon.center = CGPointMake(_playIcon.center.x, kContentH / 2);
    
    _picImgView.frame = CGRectMake(CGRectGetMaxX(_numberLabel.frame), CGRectGetMinY(_numberLabel.frame), kContentH - 20, kContentH - 20);
    
    CGSize size = [APPTools labelRectWithSize:CGSizeMake(0, 20) labelText:_sourceModel.name Font:[UIFont systemFontOfSize:15.0f]];
    
    _nameLabel.frame = CGRectMake(CGRectGetMaxX(_picImgView.frame) + 10, CGRectGetMinY(_picImgView.frame), size.width, CGRectGetHeight(_picImgView.frame) / 2);
    _nameLabel.font = [UIFont systemFontOfSize:15.0f];
    
    _singer_faviroteLabel.frame = CGRectMake(CGRectGetMinX(_nameLabel.frame), CGRectGetMaxY(_nameLabel.frame), CGRectGetMinX(_playIcon.frame) - CGRectGetMinX(_nameLabel.frame), CGRectGetHeight(_nameLabel.frame));
    _singer_faviroteLabel.font = [UIFont systemFontOfSize:13.0f];
    
    _quityIcon.frame = CGRectMake(CGRectGetMaxX(_nameLabel.frame) + 5, CGRectGetMinY(_nameLabel.frame), 30, CGRectGetHeight(_nameLabel.frame));
    
    _mvIcon.frame = CGRectMake(CGRectGetMaxX(_quityIcon.frame) + 5, CGRectGetMinY(_quityIcon.frame), CGRectGetWidth(_quityIcon.frame), CGRectGetHeight(_quityIcon.frame));
    
    _playIcon.image = [UIImage imageNamed:@"PlayPressed"];
    _mvIcon.image = [UIImage imageNamed:@"iconfont-mv-2"];
    _singer_faviroteLabel.textColor = [UIColor lightGrayColor];
    
}

- (void)setNum:(NSInteger)num
{
    _num = num;
    _numberLabel.text = [NSString stringWithFormat:@"%ld", _num];
}

- (void)setSourceModel:(DSYSongModel *)sourceModel
{
    _sourceModel = sourceModel;
    
    [_picImgView sd_setImageWithURL:[NSURL URLWithString:_sourceModel.picUrl] placeholderImage:PLACEHOLDER];
    _nameLabel.text = _sourceModel.name;
    _singer_faviroteLabel.text = [NSString stringWithFormat:@"%@ ♥️%@", _sourceModel.singerName,     [self getStringWithNumber:_sourceModel.favorites]];
    _singer_faviroteLabel.text = [NSString stringWithFormat:@"%@ ♥︎%@", _sourceModel.singerName,     [self getStringWithNumber:_sourceModel.favorites]];

    
    
    if ([_sourceModel.rightKey count] >=  4) {
        _quityIcon.image = [UIImage imageNamed:@"iconfont-sq"];
    } else {
        _quityIcon.image = [UIImage imageNamed:@"iconfont-hq"];
    }
    
    if (_sourceModel.mvList.count > 0) {
        _mvIcon.hidden = NO;
    } else {
        _mvIcon.hidden = YES;
    }
}

- (NSString *)getStringWithNumber:(NSNumber *)num {
    
    NSInteger sum = [num integerValue];
    NSString *str = @"";
    if (sum >= 10000) {
        str = [NSString stringWithFormat:@"%.2lf万", 1.0 * sum / 10000];
    } else {
        str = [NSString stringWithFormat:@"%ld", sum];
    }
    return str;
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
